package library.console;

import java.util.*;

/**
 * Created by Cheung Chun Hung on 2014/3/2.
 *
 */

public class ConsoleUtilities {
    /**
     * Menu Format<br/>
     * <pre>
     =============================
     |  Menu title               |
     =============================
     |  1. Option 1              |
     |  2. Option 2              |
     |  3. Option 3              |
     =============================
     </pre>
     * @param scanner The scanner object that use by console
     * @param menuTitle The Menu title
     * @param options The String array that store the options
     * @return An integer represent the option that user selected, it will return -1 if user input is not a number
     */
    public static int showMenu(Scanner scanner,String menuTitle, String[] options) {
        int width = 40;
        int input = -1;

        // Draw the menu

        // Create variable to store a horizontal line base on the width
        drawHeader(menuTitle, width+4);
        for(int i=0;i<options.length;i++){
            System.out.printf("|  %d: %-" + width + "s |\n",i+1,options[i]);
        }
        drawLine(width+8);
        System.out.print("Enter option: ");
        // Check the input is valid
        try{
            input = scanner.nextInt();
        } catch (InputMismatchException misMatchEx){
            System.out.println("Invalid option, your input is not a number.");
            return -1;
        } finally {
            scanner.nextLine();
        }

        if(input < 1 || input > options.length)
            System.out.printf("Invalid option, please enter a number between %s to %s.\n", 1, options.length);
        return input;
    }

    public static void printBox(String header, String message, int width){
        drawHeader(header, width);
        if(message == null)
            return;
        String messageArray[] = message.split("\n");
        for(String m:messageArray){
            System.out.printf("| %-" + width + "s |\n",m);
        }
        drawLine(width+4);
    }

    private static void drawHeader(String menuTitle, int width) {
        System.out.print("\n \n");
        drawLine(width+4);
        System.out.printf("|  %-" + width + "s|\n",menuTitle);
        drawLine(width+4);
    }

    private static void drawLine(int width) {
        char[] hrLine = new char[width];
        Arrays.fill(hrLine, '=');
        System.out.println(hrLine);
    }

    private static void drawLine(int width, char patten) {
        char[] hrLine = new char[width];
        Arrays.fill(hrLine, patten);
        System.out.println(hrLine);
    }

    public static void printTable(String title,String[] header, List<List<String>> item, int[] columnsWidth ){
        Iterator itr = item.iterator();
        int width = 0;

        // draw table title
        for(int w:columnsWidth){
            width += w;
        }
        drawHeader(title,width);

        // draw column header
        System.out.print("|  ");
        for(int i=0;i<header.length;i++){
            System.out.printf("%-"+columnsWidth[i]+"s",header[i]);
        }
        System.out.println("|");
        drawLine(width+4, '-');

        // print the content
        while (itr.hasNext()){
            List<String> l = (List)itr.next();
            System.out.print("|  ");
            for(int i=0;i<columnsWidth.length;i++){
            	String val = l.get(i);
            	if(val.length() > columnsWidth[i] -2){
            		val = val.substring(0, columnsWidth[i] -3) + "..";
            	}
                System.out.printf("%-"+columnsWidth[i]+"s",val);
            }
            System.out.println("|");
        }
        drawLine(width+4);
    }
}
